package cn.qingyun.gis.modules.init.system.service.impl;

import cn.qingyun.gis.modules.init.system.entity.dto.LogDTO;
import cn.qingyun.gis.modules.init.system.mapper.BaseCommonMapper;
import cn.qingyun.gis.modules.init.system.service.IBaseCommonService;
import cn.qingyun.gis.modules.init.system.vo.LoginUser;
import cn.qingyun.gis.utils.IPUtils;
import cn.qingyun.gis.utils.SpringContextUtils;
import cn.qingyun.gis.utils.oConvertUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author : chenwei
 * @version V1.0
 * @Project: vpclub-boot
 * @Package cn.vpclub.preresearch.modules.base.service
 * @Description: TODO
 * @date Date : 2021年01月23日 19:22
 */
@Service
public class BaseCommonServiceImpl implements IBaseCommonService {

    @Resource
    private BaseCommonMapper baseCommonMapper;

    @Async
    @Override
    public void addLog(LogDTO logDTO) {
        if (oConvertUtils.isEmpty(logDTO.getId())) {
            logDTO.setId(String.valueOf(IdWorker.getId()));
        }
        baseCommonMapper.saveLog(logDTO);
    }

    @Async
    @Override
    public void addLog(String logContent, Integer logType, Integer operatetype, LoginUser user) {
        LogDTO sysLog = new LogDTO();
        sysLog.setId(String.valueOf(IdWorker.getId()));
        //注解上的描述,操作日志内容
        sysLog.setLogContent(logContent);
        sysLog.setLogType(logType);
        sysLog.setOperateType(operatetype);
        try {
            //获取request
            HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
            //设置IP地址
            sysLog.setIp(IPUtils.getIpAddr(request));
        } catch (Exception e) {
            sysLog.setIp("127.0.0.1");
        }
        if (user != null) {
            sysLog.setUserid(user.getUsername());
            sysLog.setUsername(user.getRealname());
        }
        sysLog.setCreateTime(new Date());
        //保存系统日志
        baseCommonMapper.saveLog(sysLog);
    }

    @Async
    @Override
    public void addLog(String logContent, Integer logType, Integer operateType) {
        addLog(logContent, logType, operateType, null);
    }


}
